package com.lzx.user.controller;

import com.lzx.common.utils.R;
import com.lzx.user.entity.User;
import com.lzx.user.entity.vo.SearchParams;
import com.lzx.user.entity.vo.UserVO;
import com.lzx.user.service.UserService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lzx
 * @since 2022-10-03
 */
@RestController
@RequestMapping("/user/user")
public class UserController {
    @Resource
    private UserService userService;

    //获取所有用户昵称
    @GetMapping("/getUserNickname")
    public R getUserNickname() {
        return userService.getUserNickname();
    }

    //获取所有用户
    @GetMapping("/getUserList/{current}/{size}")
    public R getUserList(@PathVariable int current, @PathVariable int size) {
        return userService.getUserList(current, size);
    }

    //新增用户
    @PostMapping("/addUser")
    public R addUser(@RequestBody @Valid User user, BindingResult result) {
        if (result.hasErrors()) {
            return R.error().message(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return userService.addUser(user);
    }

    //修改用户信息
    @PutMapping("/updateUser")
    public R updateUser(@RequestBody @Valid UserVO userVO, BindingResult result) {
        if (result.hasErrors()) {
            return R.error().message(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return userService.updateUser(userVO);
    }

    //禁用用户
    @PutMapping("/deleteUser/{ids}")
    public R deleteUser(@PathVariable List<Long> ids) {
        return userService.deleteUser(ids);
    }

    //解禁用户
    @PutMapping("/relieveUser/{id}")
    public R relieveUser(@PathVariable Long id) {
        return userService.relieveUser(id);
    }

    //根据id获取用户信息
    @GetMapping("/{id}")
    public R getUser(@PathVariable Long id) {
        return userService.getUser(id);
    }

    //搜索
    @PostMapping("/searchUser")
    public R searchUser(@RequestBody SearchParams searchParams) {
        return userService.searchUser(searchParams);
    }

    //导出
    @GetMapping("/exportUser")
    public void exportUser(HttpServletResponse response) {
        userService.exportUser(response);
    }
}
